// ajax请求;
function xhrGet( url , data ){
      return new Promise(function( resolve ){
            var xhr = new XMLHttpRequest();
            // 发送请求之前 ，如果有data传入;
            if(typeof data === "object"){
                  url += dataToUrl( data );
            }
            xhr.open("GET", url);
            xhr.send(null);
            xhr.onload = function(){
                  if(xhr.status === 200){
                        resolve(xhr.responseText);
                  }
            }
      });
}
// 字符串处理的方法 ;
function dataToUrl( data ){
      var res = "";
      for(var attr in data){
            res += "&" + attr + "=" + data[attr];
      }
      return res.replace(/^&/,"?");
}
// cookie操作;
function getCookie(name){
      // 分号后面有空格,分号后面有空格,分号后面有空格
      var cookie_array = document.cookie.split("; ");
      // console.log(cookie_array)
      for(var i = 0 ; i < cookie_array.length ; i ++){
            // 取出每一条cookie
            var cookie_item = cookie_array[i].split("=");
            // 每一套cookie分成key和value;
            // 根据key 去获取value值;
            if(cookie_item[0] === name){
                  return decodeURIComponent(cookie_item[1]);
            }
      }
}
function setCookie(name , value , expires , path , domain){
      var cookie_str = "";
      cookie_str += name + "=" + value;
      // 表示不为空
      if(expires && typeof expires === "number"){
            // 获取一个相应日期的日期对象;
            var d = new Date();
            d.setDate(d.getDate() + expires);
            // 拼接对应的字符串;
            cookie_str += ";expires=" + d;
      }
      // str = "key=value;expires="日期
      if(path && typeof path === "string"){
            // 拼接对应的字符串;
            cookie_str += ";path=" + path;
      }
      if(domain && typeof domain === "string"){
            // 拼接对应的字符串;
            cookie_str += ";domain=" + domain;
      }
      // console.log(cookie_str);
      document.cookie = cookie_str;
}
function removeCookie( name  , path){
      setCookie(name , null , -1 ,  path )
}

class Mount{
      constructor( options ){
            //  beforeload : function(){}
            // 钩子函数一定是可有可无的!;
            this.options = options;
            typeof this.options.beforeload == "function" ? this.options.beforeload(this) : ""; 
            this.load( this.options.url , this.options.data , this.options.main );
      }
      async load( url , data , container){
            let data_str = "";
            for(let key in data){
                  data_str += "&"+key+"=" + data[key];
            }
            // 如果存在?则表示可以直接拼接 &;
            if(/\?/.test(url)){
                  url += data_str;
            }else{
                  url += data_str.replace(/^&/,"?");
            }
            // 加载数据;
            let response = await fetch(url);
            let res      = await response.json();
            let html     = this.render(res);
            typeof this.options.loaded == "function" ? this.options.loaded.call(this,res) : ""; 
            document.querySelector(container).innerHTML = html;
      }
      render(res){
            return "";
      }
}   